<?php


namespace App\Model;


use Encore\Admin\Auth\Database\Administrator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\DB;

class PurchaseOrder extends Model
{

    public $table = "purchase_order";

    protected $fillable = [
        'operator_id',
        'supplier_id'
    ];

    /**
     * @desc 关联详情
     *
     * @return HasMany
     *
     * @author: 梅杰[meijie3169@dingtalk.com] at 2020-03-10 11:40:33
     */
    public function detail()
    {
        return $this->hasMany(PurchaseOrderDetail::class);
    }

    /**
     * 关联未处理的详情
     *
     * @return HasOne
     * @author: 梅杰 2020/3/15 13:08
     */
    public function noHandleCount()
    {
        return $this->hasOne(PurchaseOrderDetail::class)
            ->where('has_handle',0)
            ->groupBy('purchase_order_id','count')
            ->select(['purchase_order_id',DB::raw("count(*) as count")]);
    }

    /**
     * 管理操作人
     * @return BelongsTo
     * @author: 梅杰 2020/3/15 11:35
     */
    public function operator()
    {
         return $this->belongsTo(Administrator::class,'operator_id');
    }

    /**
     * 管理操作人
     * @return BelongsTo
     * @author: 梅杰 2020/3/15 11:35
     */
    public function supplier()
    {
        return $this->belongsTo(Administrator::class,'supplier_id');
    }

}
